home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / WINDOWS / WSHEL202.ARJ / COPY.C < prev    next >
Text File  |  1992-05-26  |  11KB  |  398 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <direct.h>
  5. #include <dos.h>
  6. #include <time.h>
  7. #include <ctype.h>
  8. #include "wstdio.h"
  9. #include "wslib.h"
  10. #include "copy.h"
  11.  
  12. #define CF_BUFFSIZE     1000000
  13. #define CF_MAX_READ     65535
  14. static char szMemError[] = "Error allocating memory for copy\n";
  15. static char szNoSpace[] =
  16. "Insufficent disk space copying \"%s\".  Do you want to retry?\n\nNOTE: \
  17. If copying to a floppy, you can insert a new one and press \"Retry\" to \
  18. continue copying to another disk.";
  19.  
  20. HANDLE hMod;
  21.  
  22. /***************************************************************************/
  23. /* FUNCTION: LibMain - Entry function for DLL as defined by libentry.asm   */
  24. /* USES    : parameters ignored                                            */
  25. /* DESCRIPTION: this function is called by Windows to initailize the DLL   */
  26. /*-------------------------------------------------------------------------*/
  27. int FAR PASCAL LibMain (hModule, wDataSeg, cbHeapSize, lpszCmdLine)
  28.    HANDLE   hModule ;
  29.    WORD     wDataSeg ;
  30.    WORD     cbHeapSize ;
  31.    LPSTR    lpszCmdLine ;
  32. {
  33.    if (cbHeapSize != 0)
  34.        UnlockData (0);
  35.    hMod = hModule;
  36.    return 1;
  37. }
  38.  
  39. /***************************************************************************/
  40. /* FUNCTION: WEP                                                           */
  41. /* USES    : parameters ignored                                            */
  42. /* NOTES   : Function called by windows to destroy DLL                     */
  43. /*-------------------------------------------------------------------------*/
  44. void FAR PASCAL WEP (bSystemExit)
  45. int      bSystemExit ;
  46. {
  47.      return ;
  48. }
  49.  
  50. #define LINELEN 130
  51. int CreateSourceNameAndDoCopy (HWND hwndDisplay, HANDLE hCopyBuff, LPSTR szSrcCmdLine, LPSTR dest, LPSTR destcatptr);
  52.  
  53. int FAR PASCAL ModuleProc (HWND hwndDisplay, int argc, LPSTR argv[])
  54. {
  55.    HANDLE hCopyBuff;          // buffer for copy
  56.  
  57.    char szDestFName[LINELEN]; // destination filename string 
  58.    LPSTR 
  59.         dest,              // points to dest file name 
  60.         destcatptr;        // points end of dest path, where the source 
  61.                            // filename is copied 
  62.                            // if == 0, no dest filename subs done 
  63.    LPSTR lastparam;        // pointer to last parameter
  64.  
  65.    dest = szDestFName;     // points to dest file name 
  66.    destcatptr = 0;
  67.  
  68.    if (argc < 2)
  69.    {
  70.       dputs (hwndDisplay, "usage: copy <sourcefile> <destfile>\n");
  71.       return TRUE;
  72.    }
  73.  
  74.    lastparam = argv[argc-1];
  75.  
  76.    if (argc == 2)
  77.    {
  78.       // if only one parameter, use current directory for dest 
  79.       destcatptr = szDestFName; 
  80.    }
  81.    /* is destination paramter a directory? if so, dest name substituions */
  82.    else if (IsDirectory (lastparam))
  83.    {
  84.       LPSTR lpstrBkSlsh;
  85.       lstrcpy (dest, lastparam);
  86.       lpstrBkSlsh = dest + lstrlen (dest) - 1;
  87.       if (*lpstrBkSlsh != '\\' && *lpstrBkSlsh != ':')
  88.          *(++lpstrBkSlsh) = '\\';
  89.       destcatptr = lpstrBkSlsh + 1;
  90.    }
  91.  
  92.    // is it a drive letter only??
  93. /* 5/25/92 NEEDED ????
  94.    else if (isalpha (*lastparam) && *(lastparam+1) == ':' && *(lastparam+2) == NULL)
  95.    {
  96.       destcatptr = lastparam + 2;
  97.    }
  98. */
  99.  
  100.    /* destination is a single file name */
  101.    else
  102.    {
  103.       lstrcpy (dest, lastparam);
  104.    }
  105.  
  106.    // allocate and lock memory for copy
  107.    if (   (hCopyBuff = GlobalAlloc (GMEM_MOVEABLE, CF_BUFFSIZE)) != NULL
  108.        || (hCopyBuff = GlobalAlloc (GMEM_MOVEABLE, CF_BUFFSIZE / 2)) != NULL
  109.        || (hCopyBuff = GlobalAlloc (GMEM_MOVEABLE, CF_BUFFSIZE / 4)) != NULL)
  110.    {
  111.       if (GlobalLock (hCopyBuff) != NULL)
  112.       {
  113.          // Can you spot the command that does the copy in this section?
  114.          argc-=2;  argv++;
  115.          while (argc-- > 0 && CreateSourceNameAndDoCopy (hwndDisplay, hCopyBuff, *argv++, dest, destcatptr))
  116.             ;
  117.          GlobalUnlock (hCopyBuff);
  118.       } // if (GlobalLock)
  119.       else
  120.       {
  121.          dputs (hwndDisplay, szMemError);
  122.       }
  123.       GlobalFree (hCopyBuff);
  124.    } // if (GlobalAlloc)
  125.    else
  126.    {
  127.       dputs (hwndDisplay, szMemError);
  128.    }
  129.  
  130.    return TRUE;
  131. }
  132.  
  133. int CreateSourceNameAndDoCopy (HWND hwndDisplay, HANDLE hCopyBuff, LPSTR szSrcCmdLine, LPSTR dest, LPSTR destcatptr)
  134. {
  135.    BOOL bVerbose   = TRUE;    // command line flags
  136.  
  137.    char szSrcFName[LINELEN];  // destination filename string 
  138.    static char cMsg[200];     // used to print out errors 
  139.  
  140.    struct find_t fi;          // holds file info 
  141.  
  142.    LPSTR
  143.         src,               // points to source file name 
  144.         srccatptr;         // points end of src path, where the source 
  145.                            // filename is copied, also serves as flag 
  146.                            // if == 0, no source filename subs done 
  147.  
  148.    src  = szSrcFName;      // points to source file name
  149.    srccatptr  = 0; 
  150.  
  151.    lstrcpy (src, szSrcCmdLine);  // copy string to local area
  152.  
  153.    /* is parameter 1 a directory, if so, append "\*.*" if needed and perform
  154.       source name subs */
  155.    if (IsDirectory (src))
  156.    {
  157.       if (*(src + lstrlen (src) - 1) != '\\')
  158.          lstrcat (src, "\\*.*");
  159.       else
  160.          lstrcat (src, "*.*");
  161.       srccatptr = src + lstrlen (src) - 3; /* to point before appended "*.*" */
  162.    }
  163.  
  164.    // is it a drive letter only??
  165. /* 5/25/92 NEEDED???
  166.    else if (isalpha (*src) && *(src+1) == ':' && *(src+2) == NULL)
  167.    {
  168.       lstrcpy (src + 2, "*.*");
  169.       srccatptr = src + 2;
  170.    }
  171.    */
  172.  
  173.    /* is paramter 1 a wildcard (* or ?) - do name source substitution */
  174.    else if ((srccatptr = lstrchr (src, '\*')) || (srccatptr = lstrchr (src, '\?')))
  175.    {
  176.       /* srccatptr points to * or ?, but need to get it to point to end */
  177.       /* of the path if one specified, or begging of string. Done by waliking */
  178.       /* back through string */
  179.       while (srccatptr > src)    /* what a pain ! */
  180.          if (*srccatptr-- == '\\')
  181.          {
  182.             srccatptr += 2;      /* srccatptr points to char before '\\' */
  183.             break;               /* make it point to one AFTER */
  184.          }
  185.    }
  186.    /* now srccatptr is NULL or points to proper place in string */
  187.  
  188.    /* ok, the shit work aside, the copy loop is nice */
  189.    if (_dos_findfirst (src, _A_NORMAL, &fi) == 0)
  190.    {
  191.       do
  192.       {
  193.          if (destcatptr) 
  194.             lstrcpy (destcatptr, fi.name);
  195.          if (srccatptr != 0)      
  196.             lstrcpy (srccatptr, fi.name);
  197.  
  198. tryagain_butihategoto:
  199.          switch (CopyFile (hwndDisplay, hCopyBuff, dest, src))
  200.          {
  201.             case -1:
  202.             case 0:
  203.                if (bVerbose)
  204.                {
  205.                   wsprintf ((LPSTR) cMsg, "%s \t=> %s\n", (LPSTR) src, (LPSTR) dest);
  206.                   dputs (hwndDisplay, cMsg);
  207.                }
  208.             break;
  209.       
  210.             case 1:
  211.                wsprintf ((LPSTR) cMsg, "Couldn't open source file \"%s\"\n", (LPSTR) src);
  212.                dputs (hwndDisplay, cMsg);
  213.             break;
  214.  
  215.             case 10:
  216.                MessageBeep (48);       // sound for exclamation
  217.                wsprintf ((LPSTR) cMsg, (LPSTR) szNoSpace, (LPSTR) src);
  218.                if (IDRETRY == MessageBox(hwndDisplay, cMsg, "Error Copying File", MB_RETRYCANCEL | MB_ICONEXCLAMATION | MB_DEFBUTTON2))
  219.                   goto tryagain_butihategoto;
  220.                else
  221.                {
  222.                   wsprintf ((LPSTR) cMsg, "Insufficent disk space copying \"%s\".\n", (LPSTR) src);
  223.                   dputs (hwndDisplay, cMsg);
  224.                   return FALSE;
  225.                }
  226.             break;
  227.             
  228.             default:
  229.                wsprintf ((LPSTR) cMsg, "Couldn't copy \"%s\" to \"%s\"\n", (LPSTR) src, (LPSTR) dest);
  230.                dputs (hwndDisplay, cMsg);
  231.             break;
  232.          }
  233.          YieldToOthers ();
  234.       } while (_dos_findnext (&fi) == 0);
  235.    } // if (findfirst)
  236.    else
  237.    {
  238.       wsprintf ((LPSTR) cMsg, "File not found: \"%s\"\n", (LPSTR) src);
  239.       dputs (hwndDisplay, cMsg);
  240.    }
  241. }
  242.  
  243. int CopyFile (HWND hwndDisplay, HANDLE hCopyBuff, LPSTR Dest, LPSTR Src)
  244. {
  245.    char huge * lpCopyBuff;
  246.    char cMsg[LINELEN];  /* used to print out errors */
  247.    BOOL bWriteError = FALSE; 
  248.  
  249.    int hDest, hSrc,
  250.        count;
  251.  
  252.    int iChar;
  253.  
  254.    if ( (hSrc = _lopen (Src, OF_READ | OF_SHARE_DENY_WRITE)) == -1)
  255.       return 1;
  256.  
  257. /*
  258.    if (bOverWrite == FALSE)
  259.    {
  260.       if ((hDest = _lopen (Dest, OF_READ)) != -1) 
  261.       {
  262.          _lclose (hDest);
  263.          wsprintf (cMsg, "overwrite existing \"%s\"?\n", (LPSTR) Dest);
  264.          dputs (hwndDisplay, cMsg);
  265.          if (dgets (hwndDisplay, cMsg, sizeof (cMsg)) == -1)
  266.          {
  267.             _lclose (hSrc);
  268.             return -1;
  269.          }
  270.          else if (cMsg[0] != 'y')
  271.          {
  272.             _lclose (hSrc);
  273.             return 2;
  274.          }
  275.       }
  276.    }
  277. */
  278.  
  279.    if ((hDest = _lcreat (Dest, 0)) == -1) 
  280.    {
  281.       _lclose (hSrc);
  282.       return 3;
  283.    }
  284.  
  285.    lpCopyBuff = (char huge *) GlobalLock (hCopyBuff);
  286.  
  287.    while (TRUE)
  288.    {
  289.       DWORD SpaceUsed;
  290.       WORD  BytesRead, BytesToWrite;
  291.       char huge *bOut;
  292.  
  293.       //----------------------- read into buffer
  294.       SpaceUsed = 0;
  295.       while ( GlobalSize (hCopyBuff) - SpaceUsed > CF_MAX_READ )
  296.       {
  297.          YieldToOthers ();
  298.          BytesRead = _lread (hSrc, lpCopyBuff + SpaceUsed, CF_MAX_READ);
  299.          if (BytesRead == 0)
  300.             break;
  301.          SpaceUsed += BytesRead;
  302.       }
  303.  
  304.       //---------------------- write to output file
  305.       bOut = lpCopyBuff;
  306.       BytesToWrite = CF_MAX_READ;
  307.  
  308.       while ( SpaceUsed > 0 )
  309.       {
  310.          YieldToOthers ();
  311.          if (SpaceUsed < CF_MAX_READ)
  312.             BytesToWrite = (WORD) SpaceUsed;
  313.  
  314.          if (_lwrite (hDest, bOut, BytesToWrite) != BytesToWrite)
  315.          {
  316.             bWriteError = TRUE;
  317.             break;
  318.          }
  319.  
  320.          bOut      += (DWORD) BytesToWrite;
  321.          SpaceUsed -= (DWORD) BytesToWrite;
  322.       }
  323.  
  324.       if (BytesRead == 0 || bWriteError)
  325.          break;
  326.    }
  327.    GlobalUnlock (hCopyBuff);
  328.  
  329.    _lclose (hDest);
  330.    _lclose (hSrc);
  331.  
  332.    if (bWriteError)
  333.    {
  334.       static OFSTRUCT ofsShit;
  335.       OpenFile (Dest, &ofsShit, OF_DELETE);
  336.       return 10;
  337.    }
  338.  
  339.    return 0;
  340. }
  341.  
  342. int FAR PASCAL ShowOptions (HWND hwndParent)
  343. {
  344.    FARPROC lpfn;
  345.    lpfn = MakeProcInstance (OptionsDlg, hMod);
  346.    DialogBox (hMod, "OPTIONS", hwndParent, lpfn);
  347.    FreeProcInstance (lpfn);
  348.    return 0;
  349. }
  350.  
  351. int FAR PASCAL ShowAbout (HWND hwndParent)
  352. {
  353.    FARPROC lpfn;
  354.    lpfn = MakeProcInstance (AboutDlg, hMod);
  355.    DialogBox (hMod, "ABOUT", hwndParent, lpfn);
  356.    FreeProcInstance (lpfn);
  357.    return 0;
  358. }
  359.  
  360. int FAR PASCAL OptionsDlg (HWND hDlg, WORD wMsg, WORD wParam, DWORD lParam)
  361. {
  362.    switch (wMsg)
  363.    {
  364.        case WM_CLOSE:
  365.           EndDialog(hDlg, NULL);
  366.           return(TRUE);
  367.       break;
  368.  
  369.       case WM_COMMAND:
  370.          if (wParam == IDOK)
  371.              EndDialog(hDlg, NULL);
  372.           return(TRUE);
  373.       break;
  374.    }
  375.    return FALSE;
  376. }
  377.  
  378. int FAR PASCAL AboutDlg (HWND hDlg, WORD wMsg, WORD wParam, DWORD lParam)
  379. {
  380.    switch (wMsg)
  381.    {
  382.        case WM_CLOSE:
  383.           EndDialog(hDlg, NULL);
  384.           return(TRUE);
  385.       break;
  386.  
  387.       case WM_COMMAND:
  388.          if (wParam == IDOK)
  389.              EndDialog(hDlg, NULL);
  390.           return(TRUE);
  391.       break;
  392.    }
  393.    return FALSE;
  394. }
  395.  
  396.  
  397.  
  398.